import { Component, OnInit } from '@angular/core';
import { NavController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import { AuthService } from 'src/app/services/auth.service';
import { ToastService } from 'src/app/providers/toast/toast.service';
import * as constants from '../../../providers/Constants'
import { OrderService } from 'src/app/services/order.service';
import { Storage } from 'src/app/providers/Storage';
import { ShopService } from 'src/app/services/shop.service';
@Component({
  selector: 'app-order-confirm',
  templateUrl: './order-confirm.page.html',
  styleUrls: ['./order-confirm.page.scss'],
})
export class OrderConfirmPage implements OnInit {
  noAddress: boolean = false;
  addressList: any = {
    addressUserName:'',
    addressPhone:'',
    address:'',
  };
  goodsInfo: any = [];
  allPrice: number = 0;
  orderInfo: any = [];
  procPriceId: string = '';
  detailsType: number = 0;

  constructor(private nav: NavController, private order: OrderService,private activeRoute: ActivatedRoute, private auth: AuthService, private toast: ToastService) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      console.log('params.goodsInfo=', params.goodsInfo);
      // if (params.shoppingCart) {
      //   this.detailsType = 1;
      //   this.goodsInfo.procStandards=  this.goodsInfo.size

      //   this.goodsInfo = JSON.parse(params.goodsInfo);
        
      // } else {
      //   if (params.goodsInfo != undefined) {
      //     this.goodsInfo[0] = JSON.parse(params.goodsInfo);
      //     this.detailsType = params.detailsType;
      //     this.procPriceId = params.procPriceId;
      //     this.create();
      //   }
      // }

      // this.allNumber();
    });
  }
  otherAdd() {
    this.nav.navigateForward('address', {
      queryParams: {
        type: 1,
      }
    })
  }
  ionViewDidEnter() {
    let address = Storage.sessionStorage.get('addressItem');
    
    if (address) {
      console.log('address',address);

      this.addressList = address;
    }
  }
  ngOnInit() {
    console.log('----------------9');
    Storage.sessionStorage.remove('addressItem')
    this.getData();

  }
  price(type, index) {
    if (type == 0) {  //0 减
      if (this.goodsInfo[index].num <= 1) {
        return
      }
      this.allPrice = this.allPrice - this.AmountOfConversion(this.goodsInfo[index].procCashPrice)
      this.goodsInfo[index].num--
    } else {
      this.allPrice = this.allPrice + this.AmountOfConversion(this.goodsInfo[index].procCashPrice)
      this.goodsInfo[index].num++
    }
  }
  allNumber() {
    this.goodsInfo.forEach(element => {
      this.allPrice = this.allPrice + Number(element.procCashPrice) * element.num;
    });
    this.allPrice = this.AmountOfConversion(this.allPrice);
  }
  AmountOfConversion(num: number) {
    return num / 100;
  }
  create() {
    this.order.markSureOrderData({
      procId: this.goodsInfo[0].procId,
   
    }).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        this.orderInfo = res.obj;
      } else if (constants.errCode[res.retCode]) {
        this.noAddress = true;
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.noAddress = true;
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }

  getData() {
    this.auth.queryAddress({}).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        this.noAddress = false;
        let addType = false;
        for (let index = 0; index < res.data.length; index++) {
          const element = res.data[index];
          if (element.addressType == 0) {
            this.addressList = element;
            addType = true;
          }
        }
        if (!addType) {
          if (res.data.length > 0) {
            this.noAddress = true;
            this.addressList = res.data[0]
          } else {
            this.noAddress = false;
          }
        }
      } else if (res.retCode == '000025') {
        this.addressList = {};
        this.noAddress = true;
      } else if (constants.errCode[res.retCode]) {
        this.noAddress = true;
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.noAddress = true;
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }
  pay() {
    this.payConfirm();

  }

  payConfirm() {
    console.log(this.goodsInfo[0]);
    
    let data = {
      userAddressId: this.addressList.addressId,
      merchCode: this.goodsInfo[0].merchCode,
      num: this.goodsInfo[0].num,
      procAmt: this.allPrice,
      remarks: '',
      procPriceId: this.goodsInfo[0].procPriceId,
      procParts: this.orderInfo.procParts,
      procOrderType: '0',
      procGroupBuyId: '',
      procSeckillId: '',
      procStandards: this.goodsInfo[0].procStandards,
      // procColor: this.goodsInfo[0].procColor,
      procColor: 'll',
    }
    if (this.detailsType == 2) {
      data.procGroupBuyId = this.goodsInfo[0].procGroupBuyId
    }
    if (this.detailsType == 3) {
      data.procSeckillId = this.goodsInfo[0].procSeckillId
    }
    if (this.detailsType == 4) {
      data.procPriceId = this.procPriceId
    }
    this.order.markSureOrder(data).subscribe(res => {
      // 保存成功返沪上一页，否则弹出提示框
      if (res.retCode == '000000') {
        this.nav.navigateForward('pay-status', {
          queryParams: {
            payStatus: 0,   //0 成功   其他都是失败
            money: this.allPrice,//金额
            orderNumber: '',
            orderTime: '',
          }
        });
      } else if (constants.errCode[res.retCode]) {
        this.noAddress = true;
        if (constants.errCode[res.retCode] != '0') {
          this.toast.show(constants.errCode[res.retCode]);
        }
      } else if (res.retMsg) {
        this.noAddress = true;
        this.toast.show(res.retMsg);
      } else {
        this.toast.show("exception service routine");
      }
    });
  }

}
